package com.example.shushiyunsst.fragment.main.home.recycler.provider;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.example.library_common.adapter.JssBaseQuickAdapter;
import com.example.library_common.adapter.JssBaseViewHolder;
import com.example.library_common.adapter.multi.JssMultiItemViewHolder;
import com.example.library_common.utils.QMUIDeviceHelper;
import com.example.library_common.utils.QMUIDisplayHelper;
import com.example.shushiyunsst.R;
import com.example.shushiyunsst.fragment.main.home.recycler.bean.AdviseEntity;
import com.example.shushiyunsst.fragment.main.home.recycler.bean.AdviseEntityWrapper;
import com.example.wallet.global.AppGlobals;

public class AdviseProvider extends JssMultiItemViewHolder<AdviseEntityWrapper> {

    private JssBaseQuickAdapter<AdviseEntity> mAdapter;

    private PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();

    private final int height;

    public AdviseProvider(View view) {
        super(view);
        int screenWidth = QMUIDisplayHelper.getScreenWidth(AppGlobals.getApplication());
        height = screenWidth / 2;
    }

    @Override
    public int getItemLayout() {
        return R.layout.advise_provider_layout;
    }

    @Override
    public void convert(Context context, AdviseEntityWrapper adviseEntityWrapper) {
        super.convert(context, adviseEntityWrapper);
        RecyclerView mRecyclerView = findView(R.id.mRecyclerView);
        if (mRecyclerView == null) return;
        mRecyclerView.setHasFixedSize(true);
        ViewGroup.LayoutParams lp = mRecyclerView.getLayoutParams();
        if (lp != null && lp.height != height) {
            lp.height = height;
            mRecyclerView.setLayoutParams(lp);
        }
        LinearLayoutManager manager =
                new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);

        mRecyclerView.setLayoutManager(manager);

        if (mAdapter == null) {
            mAdapter = new JssBaseQuickAdapter<AdviseEntity>(R.layout.advise_provider_item_layout) {
                @Override
                protected void convert(JssBaseViewHolder helper, AdviseEntity item) {
                    super.convert(helper, item);
                    helper.setImageNetUrl(
                            R.id.img_cover,
                            item.getModUrl(),
                            R.drawable.img_erros_bg_rect
                    );
                }
            };
        }

        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setOnFlingListener(null);
        mAdapter.addData(adviseEntityWrapper.getList());
//        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
//        pagerSnapHelper.attachToRecyclerView(mRecyclerView);
    }

    @Override
    public void onViewAttachedToWindow() {
        super.onViewAttachedToWindow();
        RecyclerView mRecyclerView = findView(R.id.mRecyclerView);
        if (mRecyclerView != null) {
            pagerSnapHelper.attachToRecyclerView(mRecyclerView);
        }
    }

//    @Override
//    public void onViewDetachedFromWindow() {
//        super.onViewDetachedFromWindow();
//        RecyclerView mRecyclerView = findView(R.id.mRecyclerView);
//        if (mRecyclerView!=null){
//            pagerSnapHelper.(mRecyclerView);
//        }
//    }
}
